{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8584e922-38b6-4473-8543-46aa6e2c4750",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# <center>Python程序设计基础<center>\n",
    "## <center>第四章 Python 数据结构<center>\n",
    " <center>沈万福<center>\n",
    " <center> 办公地点：第十教学楼414室<center>\n",
    " <center>tel:18822265292<center>\n",
    " <center>mail:wfshen@tju.edu.cn<center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6adae027-3f04-46f1-8f57-2af549647be1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## <center>内容摘要<center>\n",
    "<center> 一、列表 <cetner>\n",
    "<font size = 9><center> 二、元组  <center></font>\n",
    "<center> 三、字典   <center>\n",
    "<center> 四、集合  <center>\n",
    "    <center>      五、字符串    <center>\n",
    "<center> 六、序列   <center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01d4d9f7-5a8a-4e37-bd4a-b9ccc99f27ae",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 元组\n",
    "-----\n",
    "### <font color = #DC143C> 元组的定义</font>    \n",
    "元组（tuple）和列表十分相似，最大的区别就是列表可以对元素进行添加、修改和删除，**而元组在创建之后，里面的元素不可以变更**。也就是说，元组只可以被访问，不可以被修改。从另外一个角度看，**元组就像是一个受了限制的列表**：\n",
    "- **数字、字符串等数据**，\n",
    "- **可以包含一个列表**。\n",
    "- **元组中的数据可以包含元组**。\n",
    "\n",
    "元组和列表十分相似，元组是用一对圆括号括起、用逗号分隔的多个元素的组合。元组也是序列的一种，**可以利用序列操作对元组进行处理。**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc951f08-17d3-4a96-ae9b-dde7edc3c990",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "*******************\n",
    "### <font color = #DC143C> 元组的创建</font>    \n",
    "元组的创建在形式上与列表稍有不同，列表使用中括号`[]`进行定义，而元组使用小括号`()`进行定义。元组中的元素可以是不同的数据类型，每一个元素使用`,（逗号）`进行分隔。\n",
    "- **可以使用 `=` 直接将元组赋值给一个变量，`()` 中的元组元素使用逗号进行分隔。**\n",
    "- **`tuple()`函数**\n",
    "- **如果元组中只有一个元素，则元素的后面需要加一个逗号。**\n",
    "- **定义元组的小括号`()`也可以省略，直接使用逗号分隔元素即可**。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c728e35c-30a5-419f-9253-7c7302ca6bec",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 = ('a',200,'b',150, 'c',100)\n",
    "print(tuple1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83a11cd4-3043-4391-9784-5885e5ff1182",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple2 = (3.14, 1.61, 0, -9, 6)\n",
    "tuple2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed6c6ae4-38ae-4b91-91e0-3baccdf81adc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98205d6f-62b6-410c-a6b8-f1449138eabb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "tuple3 = ('a',) #创建单一元素的元组\n",
    "tuple3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf1bf020-f328-48ad-9975-8ff82c68b1f2",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "tuple4 = 89, 53, 8080, 1024, 40    # 不加()创建元组\n",
    "tuple4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "707cedd4-4edc-4399-819a-2d876e1b29a7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "tuple_one = 432,         # 只包含一个元素的情况\n",
    "tuple_one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "db271a65-fed9-485f-a4c9-2c98996feb8f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(1, 4, 7, 10, 13)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_tuple = tuple(range(1, 15, 3))    # 使用range()函数生成的元组\n",
    "num_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "80fb62db-f6a7-472c-8927-520bd2d1fb60",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "()"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empty_tuple = ()     # 创建空元组\n",
    "empty_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "910b9a7c-5df9-4c37-b585-689b67252c37",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "('P', 'y', 't', 'h', 'o', 'n')"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_tuple = tuple(\"Python\")          # 生成元组str_tuple\n",
    "str_tuple"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7581a40b-78f8-4b1c-b575-fd5b5fe7aff6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7c44a4a-7852-4588-9799-22495d6d90bd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 元组的访问</font>  \n",
    "- 列表一样，可以通过索引、切片来访问元组的成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "efff6696-d263-4eb8-b339-da1109a02b10",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "'ef'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple1 = ('ab','cd','ef','gh') #索引\n",
    "tuple1[2] #获取元组中第三个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accc811c-c8c4-46e6-a59d-31b352e3571e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "  tuple1[-1] #获取元组中最后一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e85fe92d-6494-4455-a8f7-eece22dae616",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1[1:2] #元组切片得到的仍然是元组，得到了单元素元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7f32486-2fc1-4f86-bfb0-6c16c7445353",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1[1:3] #注意索引值的对应。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fd969c6-1103-4797-b018-6aebddb8ebd3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "531697db-1759-4d5d-9cea-bf2356af4cf0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1[1]='lm' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98b09e6a-7b37-44b2-8510-91fedc85d386",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "> 元组不能直接更改元素值!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86fcc1da-b7c4-4c5c-bcf6-cf623bb16063",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ddd16b3-4440-40ca-9793-e113dcb84a71",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 元组的“修改”</font>  \n",
    "- 这里说的“修改”并不是像修改列表元素那样，而是一种相对灵活的方式，与之前的说法并不冲突。虽然我们不能修改元组中的元素，但是我们可以**创建一个同名元组并对它重新赋值。**如果我们强行修改元组中的元素，会出现TypeError错误，提示元组对象不支持修改。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18a9ff9a-ec96-4cd6-8a35-21185ecb9c22",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 = ('ab','cd','ef')\n",
    "id(tuple1)              # 元组tuple1的内存地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad66d6c8-0d18-414c-a59a-44fe72a6ea0f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 = ('ab','cd','ef',8)\n",
    "tuple1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcce0b8d-ac17-4bb7-9c44-e480bc3b0d87",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "id(tuple1) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfb5ddc5-8f27-466c-91ee-8694026bf37c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "+ 从显示的id可以看出，虽然这两个元组的名字都是num1，但是内存地址是不同的，表示它们是两个不同的元组。id就像是它们的身份证一样，是唯一的。如果单独为元组中的一个元素重新赋值，就会出错。\n",
    "+ **使用`+`的方式拼接元组和新元素，以达到修改元组元素的目的**。比如需要在num1元组后面增加新元素，可以将num1和新元素通过+连接起来。其实这种通过拼接达到修改元组元素的方式，和之前重新定义元组的原理是相同的，即修改之前的元组id和修改之后的元组id是不相同的。\n",
    "+ **进行元组连接时，连接的内容必须都是元组**，不可以将元组与字符串或列表进行连接。如果要连接的元组只包含一个元素，不要忘记在该元素后面加上逗号。**如果想修改元组中间的元素，可以通过切片操作实现。**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff8b1311-a63a-44b9-a4df-784de86c3071",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ebf4373-a88f-44c5-89ec-fa67fe0f8994",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 元组的运算</font>  \n",
    "- 列表运算基本上都适用于元组\n",
    "  \n",
    "**(1) 元组相加：** 通过元组相加的方法生成新元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bff67252-e418-4a72-bbd0-439813e4720e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 = ('ab','cd','ef')\n",
    "tuple1 = ('gh','lm')\n",
    "tuple1+tuple2  #连接元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c6fa6df-afca-4700-8879-41f347916eee",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**(1) 元组相乘：** 用数字n乘以一个元组，会生成一个新元组。在新元组中原来的元组元素将依次被重复n次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b9fd586-a0aa-4343-826f-af591dd0efd3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 = ('ab','cd','ef')\n",
    "tuple1*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2d28baf-200a-4116-8e58-0ced5466dea6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple2 = ('ab','cd',('ef','gh'),['lm','np'])\n",
    "tuple2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c57fbaaa-349f-4a69-89a7-dc9dfcb66ca7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple2*2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22de0045-a651-400b-9637-ccbb89e63ba3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12075cbf-3feb-408d-a5b3-11b4039600a5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 元组的遍历</font>  \n",
    "- 与列表类似，也可以通过 `for` 语句或者 `while` 语句实现循环遍历元组中的所有元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad592037-656e-43e4-ae68-e93dfa937e0f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "(1) 利用`for`语句直接遍历元组中的每个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7764b9b5-eef1-4d75-86c0-cf49080417de",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 =('ab','cd','ef','gh')\n",
    "for i in tuple1:\t#直接遍历元组中的每个元素\n",
    "\t  print(i,end=' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cd208fc-66cf-44d1-9765-9ffca9c095e6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "(2) 利用`for`语句,通过索引遍历元组中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "233894e6-5d18-4f14-8e1a-d9cfd6cefb45",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 =('ab','cd','ef','gh')\n",
    "for i in range(len(tuple1)):\t#直接遍历元组中的每个元素\n",
    "\t  print(tuple1[i],end=', ')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ed6fe8-a161-4cf1-a7a9-3154dbc8abac",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "(3) 利用`while`语句,直接遍历元组中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f28e95d7-b494-40ad-92b4-824f84706fa6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1 =('ab','cd','ef','gh')\n",
    "i=0\n",
    "while i<len(tuple1):\n",
    "    print (tuple1[i],end=' ')\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3970a5e7-6eaf-419e-ac9f-193e1f62df56",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41d4ed13-5628-4d49-8130-9a8e2b3a5f25",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 列表与元组之间的转换 </font>  \n",
    "\n",
    "- **(1) 列表转换成元组**: Python中的tuple()函数可以接受一个列表，并返回一个包含同样元素的元组。从结果上看，tuple()函数冻结了列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ff34c30-86b2-4273-9d37-45337e4f9ad6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "str1=['ab','cd','ef']\n",
    "tuple(str1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a6089b7-b5d6-42e6-8701-d4cd5f8cfbb4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "- **(2) 元组转换成列表**: Python中的list()函数接受一个元组，并返回一个包含同样元素的列表。从结果上看， list()函数融化了元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cf6591a-ccf7-4a9a-b1fe-a791796d1e55",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tuple1=['ab','cd','ef']\n",
    "list(tuple1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a61296a-e4d7-4e4e-ac9a-9afc93e70ab7",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b356ae48-9ef3-4f27-9ab5-d84d6863a1db",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 元组的应用场景 </font> \n",
    "\n",
    "- **例子1**：打包和解包操作。   \n",
    "  当我们把多个用逗号分隔的值赋给一个变量时，多个值会打包成一个元组类型；当我们把一个元组赋值给多个变量时，元组会解包成多个值然后分别赋给对应的变量，如下面的代码所示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "361a2367-fdeb-4ea1-a7cf-337bb59084db",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 打包\n",
    "a = 1, 10, 100\n",
    "print(type(a), a)    # <class 'tuple'> (1, 10, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17463e4f-a4cf-4328-b94e-6158d42875e5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 解包\n",
    "i, j, k = a\n",
    "print(i, j, k)       # 1 10 100"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "203eb64f-f7ff-4cbe-95fb-446fd982bf53",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**注意**：在解包时，如果解包出来的元素个数和变量个数不对应，会引发`ValueError`异常，错误信息为：`too many values to unpack`（解包的值太多）或`not enough values to unpack`（解包的值不足）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcb23699-6d7f-40ad-a019-f99ded613126",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**一种解决变量个数少于元素的个数方法:就是使用星号表达式**。有了星号表达式，我们就可以让一个变量接收多个值，需要注意的是，用星号表达式修饰的变量会变成一个列表，列表中有0个或多个元素。还有在解包语法中，星号表达式只能出现一次。代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eac1c9f-68c1-44e6-a1fb-4d27da5a5e55",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = 1, 10, 100, 1000\n",
    "i, j, *k = a\n",
    "print(i, j, k)         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0ea9553-6503-48b6-8aae-2a58f355543d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "*i, j, k = a\n",
    "print(i, j, k)         "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a984163-929b-45e5-abcf-1b882cf2c90b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dce26501-f283-47eb-bab6-8c77df656c84",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "> 需要说明：解包语法对所有的序列都成立，这就意味着对列表以及我们之前讲到的`range`函数返回的范围序列都可以使用解包语法。如下面的代码，大家要学会借鉴。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdc08a77-d987-46d0-9782-8e8699474cdc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a, b, *c = range(1, 10)\n",
    "print(a, b, c)\n",
    "a, b, c = [1, 10, 100]\n",
    "print(a, b, c)\n",
    "a, *b, c = 'hello'\n",
    "print(a, b, c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "578dd18e-b7d8-4839-8730-f309e1316bc4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32d06528-993b-4d95-b1fd-5d7b81fc82de",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "- **例子2**：交换两个变量的值。\n",
    "     \n",
    "  交换两个变量的值是编程语言中的一个经典案例，在很多编程语言中，交换两个变量的值都需要借助一个中间变量才能做到，如果不用中间变量就需要使用比较晦涩的位运算来实现。\n",
    "\n",
    " 在Python中，交换两个变量`a`和`b`的值只需要使用如下代码：\n",
    " ```Python\n",
    "a, b = b, a\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2ac26e7-b865-4f12-915f-9fc861866ee5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "同理，如果要将三个变量`a`、`b`、`c`的值互换，即`b`赋给`a`，`c`赋给`b`，`a`赋给`c`，也可以如法炮制。\n",
    "\n",
    "```Python\n",
    "a, b, c = b, c, a\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3be20019-8e9b-4dd0-be09-beb1cf150cc8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "需要说明的是，上面并没有用到打包和解包语法。如果有多于三个变量的值要依次互换，执行的原理就是我们上面讲解的打包和解包操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d664bfbc-5c76-4415-8da3-1afb42d7c0fb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7af9efc-85e1-4dc2-a8cb-645e8eff3cfc",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 元组的应用场景 </font> \n",
    "\n",
    "1. 元组是不可变类型，**不可变类型更适合多线程环境**，因为它降低了并发访问变量的同步化开销。关于这一点，大家会在多线程的时候有更深的体会。\n",
    "\n",
    "2. 元组是不可变类型，通常**不可变类型在创建时间和占用空间上面都优于对应的可变类型**。我们可以使用`sys`模块的`getsizeof`函数来检查保存相同元素的元组和列表各自占用了多少内存空间。我们也可以使用`timeit`模块的`timeit`函数来看看创建保存相同元素的元组和列表各自花费的时间，代码如下所示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7060bf8-208d-41a4-b082-0644ea53ecf8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "   import sys\n",
    "   import timeit\n",
    "   a = list(range(100000))\n",
    "   b = tuple(range(100000))\n",
    "   print(sys.getsizeof(a), sys.getsizeof(b))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8157a6bc-0d4b-4670-923d-7db4f9517b06",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "   print(timeit.timeit('[1, 2, 3, 4, 5, 6, 7, 8, 9]'))\n",
    "   print(timeit.timeit('(1, 2, 3, 4, 5, 6, 7, 8, 9)'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "459945d8-ab65-4c27-a0b3-52ee27506e98",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "3. Python中的元组和列表是可以相互转换的。借助`list()` 函数和`tuple()`函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b158956-1093-4bfe-b19a-99556acb388d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77700ab0-cb2e-4960-84f8-cb52af799381",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**************\n",
    "### <font color = #DC143C> 简单的总结 </font>\n",
    "\n",
    "- **列表和元组都是容器型的数据类型**，即一个变量可以保存多个数据。\n",
    "  \n",
    "- **列表是可变数据类型**，**元组是不可变数据类型**，所以列表添加元素、删除元素、清空、排序等方法对于元组来说是不成立的。\n",
    "  \n",
    "- 但是列表和元组都可以进行**拼接**、**成员运算**、**索引和切片**这些操作。\n",
    "  \n",
    "- 后面我们要讲到的**字符串类型**也是这样，因为字符串就是字符按一定顺序构成的序列，在这一点上三者并没有什么区别。\n",
    "  \n",
    "- **推荐大家使用列表的生成式语法来创建列表**，它很好用，也是Python中非常有特色的语法。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc9da863-1379-4f07-b7bb-72a675984a62",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a77aa599-f4db-4a0e-9c5e-5fdcd4635a79",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "\n",
    "\n",
    "<center>本节完！<center>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}